package com.mybatis.controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.mybatis.basic.entity.AjaxJson;
import com.mybatis.basic.fileupload.FileUploadByAliOSS;
import com.mybatis.dto.UEditorJSON;
import com.mybatis.service.sys.SysPicService;

@Controller
@RequestMapping("/file")
public class FileUploadController extends FileUploadByAliOSS{

	@Resource
	private SysPicService sysPicService;

	@RequestMapping(value = "/uploadToAliOSS")
	@ResponseBody
	public AjaxJson uploadToAliOSS(@RequestParam("file")MultipartFile file){
		AjaxJson ajaxJson = new AjaxJson();
		try {
			String OSSPath = super.mediaToAliOSS(file,1);
			if(!OSSPath.equals("")){
				HashMap<String, Object> result = new HashMap<>();
				result.put("path", OSSPath);
				result.put("name", super.getFileName());
				ajaxJson.setSuccess(true);
				ajaxJson.setObj(result);
				ajaxJson.setMsg("上传图片成功");
			}else {
				ajaxJson.setSuccess(false);
				ajaxJson.setMsg("上传图片失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("程序错误，上传图片失败");
		}
		return ajaxJson;
	}

	@RequestMapping(value = "/uploadToAliOSSByuEditor")
	@ResponseBody
	public UEditorJSON uploadToAliOSSByuEditor(@RequestParam("upfile")MultipartFile file){
		UEditorJSON uEditorJSON = new UEditorJSON();
		try {
			String OSSPath = super.mediaToAliOSS(file,1);
			if(!OSSPath.equals("")){
				uEditorJSON.setOriginal(super.getFileName());
				uEditorJSON.setTitle(super.getFileName());
				uEditorJSON.setUrl(OSSPath);
				uEditorJSON.setState("SUCCESS");
			}else{
				uEditorJSON.setState("ERROR");
			}
		} catch (Exception e) {
			e.printStackTrace();
			uEditorJSON.setState("ERROR");
		}
		return uEditorJSON;
	}

	@RequestMapping(value = "/delFileByAliOSS")
	@ResponseBody
	public AjaxJson delFileByAliOSS(){
		AjaxJson ajaxJson = new AjaxJson();
		try {
			boolean result = super.delFileByAliOSS("1507909164689138.png", 1);
			ajaxJson.setSuccess(result);
			ajaxJson.setMsg(result?"删除成功":"删除失败");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("程序错误，删除失败");
		}
		return ajaxJson;
	}

	@RequestMapping(value = "/delFilesByAliOSS")
	@ResponseBody
	public AjaxJson delFilesByAliOSS(){
		AjaxJson ajaxJson = new AjaxJson();
		try {
			List<String> fileNames = new ArrayList<>();
			fileNames.add("1507910444679756.png");
			fileNames.add("1507910444679756.png");
			fileNames.add("1507910452734937.png");
			List<String> result = super.delFilesByAliOSS(fileNames, 1);
			if (result == null || result.size() == 0) {
				ajaxJson.setSuccess(true);
				ajaxJson.setMsg("删除成功");
			}else{
				ajaxJson.setSuccess(false);
				ajaxJson.setObj(result);
				ajaxJson.setMsg("删除失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(false);
			ajaxJson.setMsg("程序错误，删除失败");
		}
		return ajaxJson;
	}
	
//	//*******************上传到服务器上************************************************
//	@RequestMapping(value = "/uploadToECS")
//	@ResponseBody
//	public AjaxJson upload(@RequestParam("file")MultipartFile file){
//		AjaxJson ajaxJson = new AjaxJson();
//		//System.out.println(ajaxJson.getClass().getTypeName().substring(ajaxJson.getClass().getTypeName().lastIndexOf(".")+1));
//		try {
//			String filePath = "";
//			/**
//			 * 判断是Windows还是Linux系统
//			 * 系统不同，图片保存路径不同
//			 */
//			String osName = System.getProperty("os.name");
//			if(osName.contains("Win") || osName.contains("win")){
//				filePath = "F:\\Pictures\\images";
//			}else if(osName.contains("Linux")){
//				filePath = "usr/home/images";
//			}
//			String msg = super.beginFileUpload(filePath, file);
//			
//			SysPic sysPic = null;
//			if(msg.contains("成功")){
//				sysPic = new SysPic();
//				sysPic.setPic(super.getFilePath());
//				sysPic.setName(super.getFileName());
//				sysPic.setSourceId("");
//				sysPic.setType(1);
//				sysPic.setDel(true);
//				sysPic.setCreateTime(new Date());
//				if(!sysPicService.addPic(sysPic)){
//					msg = "文件上传成功，写入数据库失败";
//				}
//			}
//			ajaxJson.setSuccess(true);
//			ajaxJson.setMsg(msg);
//			ajaxJson.setObj(sysPic);
//		} catch (Exception e) {
//			e.printStackTrace();
//			ajaxJson.setSuccess(false);
//			ajaxJson.setMsg("上传失败");
//		}
//		return ajaxJson;
//	}
//
//	@RequestMapping(value = "/uploadimage")
//	@ResponseBody
//	public UEditorJSON uploadByCourse(@RequestParam("upfile")MultipartFile file){
//		System.out.println("11111111111111111111111");
//		UEditorJSON uEditorJSON = new UEditorJSON();
//		try {
//			String filePath = "";
//			/**
//			 * 判断是Windows还是Linux系统
//			 * 系统不同，图片保存路径不同
//			 */
//			String osName = System.getProperty("os.name");
//			if(osName.contains("Win") || osName.contains("win")){
//				filePath = "F:\\Pictures\\images";
//			}else if(osName.contains("Linux")){
//				filePath = "usr/home/images";
//			}
//			String msg = super.beginFileUpload(filePath, file);
//			
//			if(msg.contains("成功")){				
//				uEditorJSON.setOriginal(super.getFileName());
//				uEditorJSON.setTitle(super.getFileName());
//				uEditorJSON.setUrl("http://localhost/images/"+super.getFileName());
//				uEditorJSON.setState("SUCCESS");
//			}else{
//				uEditorJSON.setState("ERROR");
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//			uEditorJSON.setState("ERROR");
//		}
//		return uEditorJSON;
//	}
}
